//
//  TLHtmlMediaItem.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/28.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLHtmlMediaItem.h"
#import "TLOwner.h"

@implementation TLHtmlMediaItem

- (instancetype)init
{
    self = [super init];
    if(self){
        
    }
    return self;
}

+ (instancetype)htmlMediaItemWithType:(HtmlMediaItemType)type
{
    TLHtmlMediaItem *item = [[TLHtmlMediaItem alloc] init];
    item.type = type;
    return item;
}

+ (instancetype)htmlMediaItemWithTypeATUser:(TLOwner *)curUser mediaRange:(NSRange)curRange
{
    TLHtmlMediaItem *item = [TLHtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_ATUser];
    item.name = curUser.name;
    item.href = [NSString stringWithFormat:@"/u/%@", curUser.global_key];
    item.range = curRange;
    return item;
}

- (NSString *)displayStr
{
    NSString *displayStr;
    switch (_type) {
        case HtmlMediaItemType_Image:
            displayStr = (_showType % MediaShowTypeImage == 0)? @"[图片]": @"";
            break;
        case HtmlMediaItemType_Code:
            displayStr = (_showType % MediaShowTypeCode == 0)? [NSString stringWithFormat:@"[%@]", _code]: @"[code]";
            break;
        case HtmlMediaItemType_EmotionEmoji:
            displayStr = [NSString stringWithFormat:@"[%@]", _title];
            break;
        case HtmlMediaItemType_EmotionMonkey:
            displayStr = (_showType % MediaShowTypeMonkey == 0)? @"[洋葱猴]": @"";
            break;
        case HtmlMediaItemType_ATUser:
            displayStr = _name;
            break;
        case HtmlMediaItemType_AutoLink:
        case HtmlMediaItemType_CustomLink:
            displayStr = [_linkStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            break;
        case HtmlMediaItemType_Math:
            displayStr = @"[math]";
            break;
        default:
            displayStr = @"";
            break;
    }
    return displayStr ? displayStr : @"";
}

- (BOOL)isGif
{
    return self.type == HtmlMediaItemType_Image && [self.src rangeOfString:@".gif"].location != NSNotFound;
}

@end
